package com.mailbox.mailbox.controller;

import com.mailbox.mailbox.config.websocketConfig.WebSocketMessageServer;
import com.mailbox.mailbox.domain.Message;
import com.mailbox.mailbox.domain.SecurityUser;
import com.mailbox.mailbox.service.ReceptionService;
import com.mailbox.mailbox.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

/**
 * @author 张铭辉
 */
@RestController
@Api(tags = "前台接口")

public class ReceptionController {

    @Resource
    ReceptionService receptionService;

    @PostMapping("/addAMessage")
    @ApiOperation("增加一个留言")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "tourist_name",value = "姓名", required = false),
//            @ApiImplicitParam(name = "tourist_phone",value = "电话号", required = false),
//            @ApiImplicitParam(name = "tourist_email",value = "电子邮箱", required = false),
//            @ApiImplicitParam(name = "message_title",value = "标题", required = true),
//            @ApiImplicitParam(name = "message_type",value = "类型", required = true),
//            @ApiImplicitParam(name = "message_content",value = "内容", required = true),
//            @ApiImplicitParam(name = "creat_time",value = "留言时间", required = false),
//            @ApiImplicitParam(name = "is_delete",value = "这个不用要", required = false),
//            @ApiImplicitParam(name = "is_reply",value = "这个不用要", required = false),
//    })
    public ResponseResult addAMessage(Message message){
        return receptionService.addAMessage(message);
    }

    @GetMapping("/Visitor/Reception/findAllTypes")
    @ApiOperation("查询当前所有的留言类型")
    public ResponseResult findAllTypes(){
        return receptionService.findAllTypes();
    }

    @ApiOperation(value = "发送邮箱验证码", notes = "输入邮箱")
    @PostMapping("/Visitor/Reception/sendEmail")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "email", value = "电子邮箱",required = true),
                    @ApiImplicitParam(name = "flag", value = "邮箱类型（目前只支持@qq.com和@163.com）",required = true),
            }
    )
    public ResponseResult sendAuthCodeEmail(String email, @RequestParam(value = "flag",defaultValue = "@qq.com || @163.com") String flag){
        return receptionService.judgeEmail(email,flag);
    }

    @PostMapping("/Visitor/Reception/sendMessage")
    @ApiOperation(value = "发送信息")
    public ResponseResult sendMessage(){
        String content="有一条新的留言信息";
        WebSocketMessageServer webSocketMessageServer=new WebSocketMessageServer();
        webSocketMessageServer.send(content);
        return ResponseResult.success("成功发送");
    }

    @GetMapping("/Visitor/Reception/findAllMessages")
    @ApiOperation(value = "前台查看所有的留言信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页数", required = false),
            @ApiImplicitParam(name = "size",value = "每页的多少", required = false),
    })
    public ResponseResult findAllMessages(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "5") Integer size,@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser){
        Integer userId = securityUser.getUserId();
        if(securityUser.getUserId()==null){
            return ResponseResult.error("没有用户id");
        }
        return receptionService.findAllMessages(page, size,userId);
    }

    @GetMapping("/User/Reception/findOpenMessages")
    @ApiOperation(value = "前台查看公开的信件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页数", required = true),
            @ApiImplicitParam(name = "size",value = "每页的多少", required = true)
    })
    public ResponseResult findOpenMessages(Integer page,Integer size){
        return receptionService.findOpenMessages(page, size);
    }



    @GetMapping("/Visitor/Reception/findSuchMessages")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页数", required = false),
            @ApiImplicitParam(name = "size",value = "每页的多少", required = false),
            @ApiImplicitParam(name = "startTime",value = "开始的时间(yyyy-MM-dd HH:mm:ss)", required = false),
            @ApiImplicitParam(name = "endTime",value = "结束的时间(yyyy-MM-dd HH:mm:ss)", required = false),
            @ApiImplicitParam(name = "type",value = "类型", required = false),
    })
    @ApiOperation("根据类型或者时间查询留言（类型为空，有时间代表根据时间查询；有类型，时间空，代表类型查询，都有代表根据两者一起查询）")
    public ResponseResult findSuchMessages(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "5") Integer size, String startTime,String endTime,String type,@ApiIgnore @AuthenticationPrincipal SecurityUser securityUser){
        Integer userId = securityUser.getUserId();
        if (userId==null){
            return ResponseResult.error("用户id为空");
        }
        return receptionService.findSuchMessages(page,size,type,startTime,endTime,userId);
    }

}
